package com.sk.places.places2.presentation.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.sk.places.places2.business.service.PlaceService;
import com.sk.places.places2.service.entity.Place;
/**
 * UI layer action class which will be invoked when 
 * @author skhurana
 *
 */
public class SearchPlacesAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger("SearchPlacesAction") ;
	private String buttonName ; 
	private PlaceService placeService ; 
	private List<Place> places = new ArrayList<Place>();  
	private List<Long> selectedPlaceTypes = new ArrayList<Long>() ;  
	private String visitingMonth; 
	
	
			
	public String execute() {
		try {
			if( ( selectedPlaceTypes.size()  == 0 || "all".equalsIgnoreCase(buttonName)) && visitingMonth == null) {
				places.addAll( placeService.getAllPlaces()) ;
				return SUCCESS ;
			} else  {
				places.addAll( placeService.getPlacesByTypeAndVistingMonth(selectedPlaceTypes, visitingMonth) ); 
			}
			
			logger.info(" Total places found are " + places != null ? ""+places.size() : "0");
			ActionContext.getContext().getValueStack().set("places", places) ; 
			return SUCCESS ; 
		}
		catch(Exception e) {
			logger.severe(ExceptionUtils.getStackTrace(e)) ;
			return ERROR; 
		}
	}
	

	public String getButtonName() {
		return buttonName;
	}

	public void setButtonName(String buttonName) {
		this.buttonName = buttonName;
	}

	public PlaceService getPlaceService() {
		return placeService;
	}

	public void setPlaceService(PlaceService placeService) {
		this.placeService = placeService;
	}


	public List<Place> getPlaces() {
		return places;
	}


	public void setPlaces(List<Place> places) {
		this.places = places;
	}


	public String getVisitingMonth() {
		return visitingMonth;
	}


	public void setVisitingMonth(String visitingMonth) {
		this.visitingMonth = visitingMonth;
	}


	public List<Long> getSelectedPlaceTypes() {
		return selectedPlaceTypes;
	}


	public void setSelectedPlaceTypes(List<Long> selectedPlaceTypes) {
		this.selectedPlaceTypes = selectedPlaceTypes;
	}




}
